package com.wyix.config;

import com.wyix.device.SmartThermostat;
import com.wyix.device.Thermostat;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 恒温器自动配置类
 * 演示@ConditionalOnResource注解
 */
@Configuration
@ConditionalOnClass(Thermostat.class)
@EnableConfigurationProperties(SmartHomeConfig.class)
public class ThermostatAutoConfiguration {

    private final SmartHomeConfig properties;

    public ThermostatAutoConfiguration(SmartHomeConfig properties) {
        this.properties = properties;
    }

    /**
     * 智能恒温器配置
     * @ConditionalOnResource: 当classpath存在thermostat.properties资源时生效
     */
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnResource(resources = "classpath:thermostat.properties")
    public Thermostat smartThermostat() {
        return new SmartThermostat(
                properties.getThermostat().getDefaultTemp(),
                properties.getThermostat().isAutoAdjust()
        );
    }
}